﻿Imports System.Windows.Forms
Public Class SettingsPropertyEditor
  Private _setting As SettingsEntry = Nothing
  Private _intValue As Integer = 0
  Private _stringValue As String = String.Empty
  Private _lastWasInc As Boolean = False

  Public Sub New()
    InitializeComponent()
    Me.InputPanelMain_EnabledChanged(Me, EventArgs.Empty)
  End Sub

  Public Overrides Property BackColor() As System.Drawing.Color
    Get
      Return MyBase.BackColor
    End Get
    Set(ByVal value As System.Drawing.Color)
      MyBase.BackColor = value
      Me.PanelCombo.BackColor = value
      Me.PanelFile.BackColor = value
      Me.PanelSetting.BackColor = value
      Me.WP7ButtonBoolean.BackColor = value
      Me.WP7ButtonDec.BackColor = value
      Me.WP7ButtonInc.BackColor = value
      Me.WP7ButtonInteger.BackColor = value
      Me.WP7ButtonSelectFile.BackColor = value
      Me.LabelSettingName.BackColor = value
      Me.LabelXLongDesc.BackColor = value
    End Set
  End Property

  Public Property BackColorTextBox() As Color
    Get
      Return Me.TextBoxPathFile.BackColor
    End Get
    Set(ByVal value As Color)
      Me.TextBoxPathFile.BackColor = value
      Me.ComboBoxRadio.BackColor = value
    End Set
  End Property

  Public Overrides Property ForeColor() As System.Drawing.Color
    Get
      Return MyBase.ForeColor
    End Get
    Set(ByVal value As System.Drawing.Color)
      MyBase.ForeColor = value
      Me.TextBoxPathFile.ForeColor = value
      Me.WP7ButtonBoolean.ForeColor = value
      Me.WP7ButtonDec.ForeColor = value
      Me.WP7ButtonInc.ForeColor = value
      Me.WP7ButtonInteger.ForeColor = value
      Me.WP7ButtonSelectFile.ForeColor = value
      Me.ComboBoxRadio.ForeColor = value
      Me.LabelSettingName.ForeColor = value
      Me.LabelXLongDesc.ForeColor = value
    End Set
  End Property

  Public Sub New(ByVal se As SettingsEntry)
    Me.New()
    Me.Setting = se
    Me.LabelSettingName.Text = se.Text
    Me.LabelXLongDesc.Text = Language.GetLanguageStringNoMissing(Me.Setting.Schema.SettingsKey & "_LongDesc")
    Me.LabelXLongDesc.ShowScrollArrows = True
    Me.LabelXLongDesc.ShowTopLine = True
    Me._intValue = Me.Setting.IntValue
    Me._stringValue = Me.Setting.StringValue

    Select Case Me.Setting.Schema.SchemaType
      Case SettingsEntry.SchemaSettings.SchemaTypeEnum.BOOL
        Me.WP7ButtonBoolean.Enabled = True
        Me.WP7ButtonBoolean.Visible = True
        Me.WP7ButtonBoolean.BringToFront()
        Me.SetBooleanString()

      Case SettingsEntry.SchemaSettings.SchemaTypeEnum.INT
        Me.WP7ButtonDec.Enabled = True
        Me.WP7ButtonDec.Visible = True
        Me.WP7ButtonDec.BringToFront()
        Me.WP7ButtonInc.Enabled = True
        Me.WP7ButtonInc.Visible = True
        Me.WP7ButtonInc.BringToFront()
        Me.WP7ButtonInteger.Enabled = True
        Me.WP7ButtonInteger.Visible = True
        Me.WP7ButtonInteger.BringToFront()
        Me.WP7ButtonInteger.Text = Me._intValue.ToString

      Case SettingsEntry.SchemaSettings.SchemaTypeEnum.LANGUAGE
        Me.PanelCombo.Enabled = True
        Me.PanelCombo.Visible = True
        Me.PanelCombo.BackColor = Me.BackColor
        Me.PanelCombo.BringToFront()
        Me.ComboBoxRadio.BackColor = Me.BackColor
        Me.ComboBoxRadio.ForeColor = Me.ForeColor

        For Each s As String In Settings.AllLanguages
          If String.Compare(Me._setting.StringValue.Trim, s.Trim, True) = 0 Then
            Me.ComboBoxRadio.SelectedIndex = Me.ComboBoxRadio.Items.Add(s)
          Else
            Me.ComboBoxRadio.Items.Add(s)
          End If
        Next

      Case SettingsEntry.SchemaSettings.SchemaTypeEnum.FILE
        Me.PanelFile.Enabled = True
        Me.PanelFile.Visible = True
        Me.PanelFile.BringToFront()
        Me.WP7ButtonSelectFile.Enabled = True
        Me.WP7ButtonSelectFile.Visible = True
        Me.WP7ButtonSelectFile.BringToFront()
        Me.TextBoxPathFile.Text = Me._stringValue
        Me.TextBoxPathFile.SelectionStart = Me.TextBoxPathFile.Text.Length  ' Scroll the input box all the way to the right
        Me.TextBoxPathFile.SelectionLength = 0
        Me.TextBoxPathFile.Focus()

      Case SettingsEntry.SchemaSettings.SchemaTypeEnum.FOLDERS, SettingsEntry.SchemaSettings.SchemaTypeEnum.STRING_LIST
        If Me._stringValue IsNot Nothing AndAlso Me._stringValue.Length > 0 Then
          Dim strs() As String = Me._stringValue.Split(CChar(IIf(Me.Setting.Schema.SchemaType = SettingsEntry.SchemaSettings.SchemaTypeEnum.FOLDERS, ":"c, vbCr)))
          For Each s As String In strs
            s = s.Trim
            If s.Length > 0 Then
              Me.LabelXLongDesc.Text &= vbCrLf & s
            End If
          Next
        Else
          Me.LabelXLongDesc.Text &= vbCrLf & Language.GetLanguageString("NoItemsInList")
        End If
        Me.WP7ButtonBoolean.Enabled = True
        Me.WP7ButtonBoolean.Visible = True
        Me.WP7ButtonBoolean.Text = Language.GetLanguageString("Edit")
        Me.WP7ButtonBoolean.BringToFront()

      Case SettingsEntry.SchemaSettings.SchemaTypeEnum.RADIO
        Me.PanelCombo.Enabled = True
        Me.PanelCombo.Visible = True
        Me.PanelCombo.BackColor = Me.BackColor
        Me.PanelCombo.BringToFront()
        Me.ComboBoxRadio.BackColor = Me.BackColor
        Me.ComboBoxRadio.ForeColor = Me.ForeColor
        For Each re As SettingsEntry.SchemaSettings.RadioEntry In Me._setting.Schema.RadioButtons
          If String.Compare(Me._setting.StringValue.Trim, re.Value.Trim, True) = 0 Then
            Me.ComboBoxRadio.SelectedIndex = Me.ComboBoxRadio.Items.Add(re)
          Else
            Me.ComboBoxRadio.Items.Add(re)
          End If
        Next

      Case SettingsEntry.SchemaSettings.SchemaTypeEnum.REGEX_PATH
        ' For now we'll just do a text entry, but in the future, this will be more complete, with regex preview
        Me.PanelFile.Enabled = True
        Me.PanelFile.Visible = True
        Me.WP7ButtonSelectFile.Enabled = False
        Me.WP7ButtonSelectFile.Visible = False
        Me.TextBoxPathFile.Width = Me.PanelFile.Width - 2
        Me.PanelFile.BringToFront()
        Me.TextBoxPathFile.Text = Me._stringValue
        Me.TextBoxPathFile.Focus()


      Case SettingsEntry.SchemaSettings.SchemaTypeEnum.SKIN
        Me.PanelCombo.Enabled = True
        Me.PanelCombo.Visible = True
        Me.PanelCombo.BackColor = Me.BackColor
        Me.PanelCombo.BringToFront()
        Me.ComboBoxRadio.BackColor = Me.BackColor
        Me.ComboBoxRadio.ForeColor = Me.ForeColor

        For Each s As String In Settings.AllSkins
          If String.Compare(Me._setting.StringValue.Trim, s.Trim, True) = 0 Then
            Me.ComboBoxRadio.SelectedIndex = Me.ComboBoxRadio.Items.Add(s)
          Else
            Me.ComboBoxRadio.Items.Add(s)
          End If
        Next
      Case SettingsEntry.SchemaSettings.SchemaTypeEnum.CONTROL_EDITOR
        ' Do nothing; Control editor is spawned by calling process; Nothing to do here

      Case Else
        'Case SettingsEntry.SchemaSettings.SchemaTypeEnum.CONTROL_EDITOR, SettingsEntry.SchemaSettings.SchemaTypeEnum.STREAMREGEX_EDITOR, SettingsEntry.SchemaSettings.SchemaTypeEnum.SUBSCRIPTION_EDITOR
        ' TODO: These are handled separately and not within the property editor skeleton; They are not yet implemented, so warn the user and exit
        MessageBox.Show(Language.GetLanguageString("NotImplemented"), "Avian Play", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
        Me.TimerClose.Enabled = True

    End Select
  End Sub

  Public Property Setting() As SettingsEntry
    Get
      Return Me._setting
    End Get
    Set(ByVal value As SettingsEntry)
      Me._setting = value
      Me._intValue = Me._setting.IntValue
      Me._stringValue = Me._setting.StringValue
    End Set
  End Property

  Public ReadOnly Property Value() As String
    Get
      Select Case Me.Setting.Schema.SchemaType
        Case SettingsEntry.SchemaSettings.SchemaTypeEnum.BOOL, SettingsEntry.SchemaSettings.SchemaTypeEnum.INT
          Return Me._intValue.ToString

        Case SettingsEntry.SchemaSettings.SchemaTypeEnum.FILE, SettingsEntry.SchemaSettings.SchemaTypeEnum.FOLDERS, SettingsEntry.SchemaSettings.SchemaTypeEnum.STRING_LIST, SettingsEntry.SchemaSettings.SchemaTypeEnum.REGEX_PATH
          Return Me._stringValue

        Case SettingsEntry.SchemaSettings.SchemaTypeEnum.RADIO, SettingsEntry.SchemaSettings.SchemaTypeEnum.SKIN, SettingsEntry.SchemaSettings.SchemaTypeEnum.LANGUAGE
          Dim o As Object = Me.ComboBoxRadio.SelectedItem
          If o IsNot Nothing Then
            If o.GetType Is GetType(SettingsEntry.SchemaSettings.RadioEntry) Then
              Return CType(o, SettingsEntry.SchemaSettings.RadioEntry).Value
            ElseIf o.GetType Is GetType(String) Then
              Return CStr(o)
            Else
              Return o.ToString
            End If
          End If

      End Select

      Return ""
    End Get
  End Property





  Private Sub WP7ButtonBoolean_MouseUp(ByVal sender As System.Object, ByVal e As MouseEventArgs) Handles WP7ButtonBoolean.MouseUp
    If Me._setting.Schema.SchemaType = SettingsEntry.SchemaSettings.SchemaTypeEnum.FOLDERS Then
      ' We're using this button to enter the edit mode for folders
      Dim strl As New List(Of String)
      If Me._stringValue IsNot Nothing AndAlso Me._stringValue.Length > 0 Then
        Dim strs() As String = Me._stringValue.Split(":")
        For Each s As String In strs
          s = s.Trim
          If s.Length > 0 Then
            strl.Add(s)
          End If
        Next
      End If
      Dim fle As New FolderListEditor(strl)
      fle.BackColor = Me.BackColor
      fle.ForeColor = Me.ForeColor
      If fle.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
        Dim first As Boolean = True
        Me.LabelXLongDesc.Text = Language.GetLanguageStringNoMissing(Me.Setting.Schema.SettingsKey & "_LongDesc")
        Me._stringValue = ""
        For Each s As String In fle.Folders
          s = s.Trim
          If s.Length > 0 Then
            Me.LabelXLongDesc.Text &= vbCrLf & s
          End If
          If first Then
            Me._stringValue = s
            first = False
          Else
            Me._stringValue &= ":" & s
          End If
        Next
        If Me._stringValue.Length <= 0 Then
          Me.LabelXLongDesc.Text &= vbCrLf & Language.GetLanguageString("NoItemsInList")
        End If
      End If
    ElseIf Me._setting.Schema.SchemaType = SettingsEntry.SchemaSettings.SchemaTypeEnum.STRING_LIST Then
      ' We're using this button to enter the edit mode for strings
      Dim strl As New List(Of String)
      If Me._stringValue IsNot Nothing AndAlso Me._stringValue.Length > 0 Then
        Dim strs() As String = Me._stringValue.Split(vbCr)
        For Each s As String In strs
          s = s.Trim
          If s.Length > 0 Then
            strl.Add(s)
          End If
        Next
      End If
      Dim sle As New StringListEditor(strl)
      sle.BackColor = Me.BackColor
      sle.ForeColor = Me.ForeColor
      If sle.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
        Dim first As Boolean = True
        Me.LabelXLongDesc.Text = Language.GetLanguageStringNoMissing(Me.Setting.Schema.SettingsKey & "_LongDesc")
        Me._stringValue = ""
        For Each s As String In sle.Strings
          s = s.Trim
          If s.Length > 0 Then
            Me.LabelXLongDesc.Text &= vbCrLf & s
          End If
          If first Then
            Me._stringValue = s
            first = False
          Else
            Me._stringValue &= vbCr & s
          End If
        Next
        If Me._stringValue.Length <= 0 Then
          Me.LabelXLongDesc.Text &= vbCrLf & Language.GetLanguageString("NoItemsInList")
        End If
      End If
    Else
      If Me._intValue = 0 Then
        Me._intValue = 1
      Else
        Me._intValue = 0
      End If
      Me.SetBooleanString()
    End If
  End Sub

  Private Sub WP7ButtonInteger_MouseUp(ByVal sender As System.Object, ByVal e As MouseEventArgs) Handles WP7ButtonInteger.MouseUp
    Dim np As New NumPad
    np.Text = Me._intValue.ToString

    If np.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
      Me._intValue = np.ValueInteger

      If Me._intValue > Me._setting.Schema.IntMaximum Then
        Me._intValue = Me._setting.Schema.IntMaximum
      ElseIf Me._intValue < Me._setting.Schema.IntMinimum Then
        Me._intValue = Me._setting.Schema.IntMinimum
      End If

      Me.WP7ButtonInteger.Text = Me._intValue.ToString
    End If
  End Sub

  Private Sub WP7ButtonInc_MouseDown(ByVal sender As System.Object, ByVal e As MouseEventArgs) Handles WP7ButtonInc.MouseDown
    Me._intValue += Me._setting.Schema.IntStep

    If Me._intValue > Me._setting.Schema.IntMaximum Then
      Me._intValue = Me._setting.Schema.IntMaximum
    End If

    Me.WP7ButtonInteger.Text = Me._intValue.ToString

    If Not Me.TimerRepeat.Enabled Then
      Me._lastWasInc = True
      Me.TimerRepeat.Interval = 500
      Me.TimerRepeat.Enabled = True
    End If
  End Sub

  Private Sub WP7ButtonDec_MouseDown(ByVal sender As System.Object, ByVal e As MouseEventArgs) Handles WP7ButtonDec.MouseDown
    Me._intValue -= Me._setting.Schema.IntStep

    If Me._intValue < Me._setting.Schema.IntMinimum Then
      Me._intValue = Me._setting.Schema.IntMinimum
    End If

    Me.WP7ButtonInteger.Text = Me._intValue.ToString

    If Not Me.TimerRepeat.Enabled Then
      Me._lastWasInc = False
      Me.TimerRepeat.Interval = 500
      Me.TimerRepeat.Enabled = True
    End If
  End Sub

  Private Sub WP7ButtonDecInc_MouseUp(ByVal sender As System.Object, ByVal e As MouseEventArgs) Handles WP7ButtonInc.MouseUp, WP7ButtonDec.MouseUp
    Me.TimerRepeat.Enabled = False
  End Sub

  Private Sub SetBooleanString()
    If Me._intValue <> 0 Then
      Me.WP7ButtonBoolean.Text = Language.GetLanguageString("True")
    Else
      Me.WP7ButtonBoolean.Text = Language.GetLanguageString("False")
    End If
  End Sub

  Private Sub MenuItemOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemOK.Click
    ' Restart warning
    If Me.Setting.Schema.RestartRequired Then
      MessageBox.Show(Language.GetLanguageString("SettingsRestartWarning"), "Avian Play", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
    End If

    ' Update the default value in the settings schema cache because this may not be reloaded before going into this setting again
    Me.Setting.IntValue = Me._intValue
    Me.Setting.StringValue = Me._stringValue

    ' Exit with result "OK"
    Me.DialogResult = Windows.Forms.DialogResult.OK
    Me.Close()
  End Sub

  Private Sub MenuItemCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemCancel.Click
    Me.DialogResult = Windows.Forms.DialogResult.Cancel
    Me.Close()
  End Sub

  Private Sub TimerRepeat_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerRepeat.Tick
    Me.TimerRepeat.Interval = 50

    If Me._lastWasInc Then
      Me.WP7ButtonInc_MouseDown(Me, New MouseEventArgs(Windows.Forms.MouseButtons.None, 1, 0, 0, 0))
    Else
      Me.WP7ButtonDec_MouseDown(Me, New MouseEventArgs(Windows.Forms.MouseButtons.None, 1, 0, 0, 0))
    End If
  End Sub

  Private Sub WP7ButtonSelectFile_MouseUp(ByVal sender As System.Object, ByVal e As MouseEventArgs) Handles WP7ButtonSelectFile.MouseUp
    Dim sf As New SelectFile
    If sf.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
      If sf.Filename.Length <= 0 Then
        MessageBox.Show(Language.GetLanguageString("SettingsBadFilename"), "Avian Play")
      Else
        Me.TextBoxPathFile.Text = sf.Path

        ' Scroll the input box all the way to the right
        Me.TextBoxPathFile.SelectionStart = Me.TextBoxPathFile.Text.Length
        Me.TextBoxPathFile.SelectionLength = 0
        Me.TextBoxPathFile.Focus()
      End If
    End If
  End Sub

  Private Sub InputPanelMain_EnabledChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InputPanelMain.EnabledChanged
    If Me.InputPanelMain.Enabled Then
      Me.PanelPushUp.Height = Me.InputPanelMain.Bounds.Height
    Else
      Me.PanelPushUp.Height = 0
    End If
  End Sub

  Private Sub TimerClose_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerClose.Tick
    Me.TimerClose.Enabled = False
    Me.Close()
  End Sub
End Class